#include "mainwindow.h"
#include "myview.h"
#include "tabwidget.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)

{
    newqtab = new tabWidget(this);
    setWindowTitle("MEDUSA");
    resize(800,400);
    disegna();
}

tabWidget *MainWindow::tWidget() const
{
    return newqtab;
}

myWebView *MainWindow::currentTab() const
{
    return newqtab->currentWebView();
}

void MainWindow::loadFile(const QString &fileName)
{
     QFile file(fileName);
     if (!file.open(QFile::ReadOnly | QFile::Text)) {
         QMessageBox::warning(this, tr("Application"),
                              tr("Cannot read file %1:\n%2.")
                              .arg(fileName)
                              .arg(file.errorString()));
         return;
     }
/*
     QTextStream in(&file);
     QApplication::setOverrideCursor(Qt::WaitCursor);
     textEdit->setPlainText(in.readAll());
     QApplication::restoreOverrideCursor();

     setCurrentFile(fileName);
     statusBar()->showMessage(tr("File loaded"), 2000);
*/
 }

bool MainWindow::saveFile(const QString &fileName)
 {
     QFile file(fileName);
     if (!file.open(QFile::WriteOnly | QFile::Text)) {
         QMessageBox::warning(this, tr("Application"),
                              tr("Cannot write file %1:\n%2.")
                              .arg(fileName)
                              .arg(file.errorString()));
         return false;
     }
/*
     QTextStream out(&file);
     QApplication::setOverrideCursor(Qt::WaitCursor);
     out << textEdit->toPlainText();
     QApplication::restoreOverrideCursor();
     //setCurrentFile(fileName);
     statusBar()->showMessage(tr("File saved"), 2000);
*/
     return true;
 }

void MainWindow::disegna()
{

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
    QFileDialog qfd;
    QFile qf;
//    QString zfileName = QFileDialog::getOpenFileName(this);
//*
    QString yfileName = qfd.getOpenFileName(this, tr("Browse Files"),
                                                QDir::homeDirPath(),
                                                tr("All Files (*.*)")); // CHECKITOUT
        if (!yfileName.isEmpty())
            loadFile(yfileName);
//*/
//    QString fileName = QFileDialog::getSaveFileName(this);
//        if (!fileName.isEmpty())
//            saveFile(fileName);
/*
        QString yyfileName = qfd.getOpenFileName(this, tr("Open File"),
                                                QDir::homeDirPath(),
                                                //"c:",
                                                tr("All Files (*.*)")); // CHECKITOUT
*/
    //QString xfileName = QFileDialog::getSaveFileName(this, tr("Save File"),
    //                        "testo",
    //                        tr("Images (*.png *.xpm *.jpg)"));
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

    QHBoxLayout *affiancati = new QHBoxLayout;
    myviewzone = new myWebView();
    //myviewzone->load(QUrl("http://www.javascript-coder.com/window-popup/javascript-window-open-example1.html"));
    //myviewzone->load(QUrl("http://docs.google.com/"));
    //myviewzone->load(QUrl("http://google.com/"));
    myviewzone->load(QUrl("http://www.pimco.com/TopNav/Home/Default.htm"));
    myviewzone->load(QUrl("http://code.google.com"));
    myviewzone->show();
    newqtab->addTab(myviewzone,"sex");
    affiancati->addWidget(newqtab);
    QFrame *frame = new QFrame;
    frame->setLayout(affiancati);
    setCentralWidget(frame);
}
